package com.education.service.impl;

import com.education.common.Const;
import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.ShardedJedis;

import javax.annotation.Resource;

/**
 * @author 林心晨
 * @Title:
 * @Package
 * @Description:
 * @date create in 5.7  16:18
 */
@Component("sysCacheService")
public class SysCacheService {

    private static Logger log = LoggerFactory.getLogger(SysCacheService.class);

    @Resource
    private RedisPool redisPool;

    public void saveCache(String toSavedValue, int timeoutSeconds, Const.CacheKeyConstants prefix) {
        saveCache(toSavedValue, timeoutSeconds, prefix, null);
    }
    public void saveCache(String toSavedValue, int timeoutSeconds, Const.CacheKeyConstants prefix, String... keys) {
        if (toSavedValue == null) {
            return;
        }
        ShardedJedis shardedJedis = null;
        try {
            String cacheKey = generateCacheKey(prefix, keys);
            shardedJedis = redisPool.instance();
            shardedJedis.setex(cacheKey, timeoutSeconds, toSavedValue);
        } catch (Exception e) {
            log.error("save cache exception, prefix:{}", prefix, e);
        } finally {
            redisPool.safeClose(shardedJedis);
        }
    }

    public void saveCache(String toSavedValue, int timeoutSeconds, String prefix, String... keys) {
        if (toSavedValue == null) {
            return;
        }
        ShardedJedis shardedJedis = null;
        try {
            String cacheKey = generateCacheKey(prefix, keys);
            shardedJedis = redisPool.instance();
            shardedJedis.setex(cacheKey, timeoutSeconds, toSavedValue);
        } catch (Exception e) {
            log.error("save cache exception, prefix:{}", prefix, e);
        } finally {
            redisPool.safeClose(shardedJedis);
        }
    }

    public String getFromCache(Const.CacheKeyConstants prefix, String... keys) {
        ShardedJedis shardedJedis = null;
        String cacheKey = generateCacheKey(prefix, keys);
        try {
            shardedJedis = redisPool.instance();
            String value = shardedJedis.get(cacheKey);
            return value;
        } catch (Exception e) {
            log.error("get from cache exception, prefix:{}", prefix, e);
            return null;
        } finally {
            redisPool.safeClose(shardedJedis);
        }
    }

    public String get(String prefix) {
        ShardedJedis shardedJedis = null;
        try {
            shardedJedis = redisPool.instance();
            String value = shardedJedis.get(prefix);
            return value;
        } catch (Exception e) {
            log.error("exists from cache exception, prefix:{}", prefix, e);
            return null;
        } finally {
            redisPool.safeClose(shardedJedis);
        }
    }

    public void del(String prefix) {
        ShardedJedis shardedJedis = null;
        try {
            shardedJedis = redisPool.instance();
            shardedJedis.del(prefix);
        } catch (Exception e) {
            log.error("del from cache exception, prefix:{}", prefix, e);
        } finally {
            redisPool.safeClose(shardedJedis);
        }
    }

    private String generateCacheKey(Const.CacheKeyConstants prefix, String... keys) {
        String key = prefix.name();
        if (keys != null && keys.length > 0) {
            key += "_" + Joiner.on("_").join(keys);
        }
        return key;
    }

    private String generateCacheKey(String prefix, String... keys) {
        String key = prefix;
        if (keys != null && keys.length > 0) {
            key += "_" +  Joiner.on("_").join(keys);
        }
        return key;
    }
}
